<?php
class transaksibankpembelian_model extends CI_Model{
	private $NoFaktur;
    private $DateFaktur;
	private $TotalQuantity;
    private $TotalAmount;
  	private $Keterangan;
	
	private $IDdetail;
	private $NoPo;
	private $KodeDetailBarang;
	private $Quantity;
    private $Discount;
    private $Amount;
    private $Createdby;
    private $Createdon;
    private $Lastupdateby;
  	private $Lastupdateon;
  	private $Active;
	
	function setNoFaktur($nofaktur){
		$this->NoFaktur = $nofaktur;
	}
	function getNoFaktur(){
		return $this->NoFaktur;
	}
	
	function setDateFaktur($datefaktur){
		$this->DateFaktur = $datefaktur;
	}
	function getDateFaktur(){
		return $this->DateFaktur;
	}
	
	function setTotalQuantity($ttlqty){
		$this->TotalQuantity = $ttlqty;
	}
	function getTotalQuantity(){
		return $this->TotalQuantity;
	}
	
	function setTotalAmount($ttlamount){
		$this->TotalAmount = $ttlamount;
	}
	function getTotalAmount(){
		return $this->TotalAmount;
	}
	
	function setKeterangan($ket){
		$this->Keterangan = $ket;
	}
	function getKeterangan(){
		return $this->Keterangan;
	}
	
	function setNoPo($nopo){
		$this->NoPo = $nopo;
	}
	function getNoPo(){
		return $this->NoPo;
	}
	
	function setIDdetail($IDdetail){
		$this->IDdetail = $IDdetail;
	}
	function getIDdetail(){
		return $this->IDdetail;
	}
	
	function setKodeDetailBarang($kddetbarang){
		$this->KodeDetailBarang = $kddetbarang;
	}
	function getKodeDetailBarang(){
		return $this->KodeDetailBarang;
	}
	
	function setQuantity($QTY){
		$this->Quantity = $QTY;
	}
	function getQuantity(){
		return $this->Quantity;
	}
	
	function setDiscount($discount){
		$this->Discount = $discount;
	}
	function getDiscount(){
		return $this->Discount;
	}
	
	function setAmount($amount){
		$this->Amount = $amount;
	}
	function getAmount(){
		return $this->Amount;
	}
	
	function setCreatedBy($c_by){
		$this->createdby = $c_by;
	}

	function getCreatedBy(){
		return $this->createdby;
	}
	function setCreatedOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->createdon = $dateNow;
	}

	function getCreatedOn(){
		return $this->createdon;
	}
	
	function setLastUpdateBy($u_by){
		$this->lastupdateby = $u_by;
	}
	function getLastUpdateBy(){
		return $this->lastupdateby;
	}
	
	function setLastUpdateOn(){
		$dateNow = date('Y-m-d H:i:s');
		$this->lastupdateon = $dateNow;
	}
	function getLastUpdateOn(){
		return $this->lastupdateon;
	}

	function setActive($active_val){
		$this->active = $active_val;
	}
	function getActive(){
		return $this->active;
	}
	
	// Call the module constructor
	function __construct(){
		parent::__construct();
	}
	
	function getkodenofak()
	{
		$query = $this->db->query("select MAX(RIGHT(NoFaktur,4)) as kdfaktur from faktur_product_pembelian");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $idfaktur)
			{
				$tmpfaktur = ((int)$idfaktur->kdfaktur)+1;
				$kd = sprintf("%04s", $tmpfaktur);
			}
			
		}
		else
		{
			$kd = "0001";
		}
		
		$kd="FK".$kd;
		return $kd;
	}
	
	function getdetailid()
	{
		$query = $this->db->query("select MAX(IDdetail) as kddetailid from transaction_faktur_product_pembelian");
		$kd = "";
		
		if($query-> num_rows() > 0)
		{
			foreach($query->result() as $detailid)
			{
				$kd = ((int)$detailid->kddetailid)+1;
			}
			
		}
		else
		{
			$kd = "1";
		}
		
		return $kd;
	}
	
	function insert()
	{
		$data = array(
			'NoFaktur' => $this->getNoFaktur(),
			'DateFaktur' => $this->getDateFaktur(),
			'TotalQuantity' => $this->getTotalQuantity(),
			'TotalAmount' => $this->getTotalAmount(),
			'Keterangan' => $this->getKeterangan(),
			'Createdby' => $this->getCreatedBy(),
			'Createdon' => $this->getCreatedOn(),
			'Lastupdateby' => 'null',
			'Lastupdateon' => 'null',
			'Active' => '1'						
		);
		$this->db->insert('faktur_product_pembelian',$data);
	}
	
	function insertdetail()
	{		
		$data = array(
			'NoFaktur' => $this->getNoFaktur(),
			'IDdetail' => $this->getIDdetail(),
			'NoPo' => $this->getNoPo(),
			'KodeDetailBarang' => $this->getKodeDetailBarang(),
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => '0',
			'Amount' => $this->getAmount(),
			'Active' => '1'					
		);
		$this->db->insert('transaction_faktur_product_pembelian',$data);
	}
	
	function update(){
		$data = array(
				'TotalQuantity' => $this->getTotalQuantity(),
				'TotalAmount' => $this->getTotalAmount(),
				'Keterangan' => $this->getKeterangan(),
				'Lastupdateby' => $this->getLastupdateby(),
				'Lastupdateon' => $this->getLastupdateon()
                
            );
 
        $this->db->where(array('NoFaktur' => $this->getNoFaktur()));
        $this->db->update('faktur_product_pembelian', $data);		
	}
	
	function updatedetail(){
		
		$data1 = array(
			'Quantity' => $this->getQuantity(),
			'Discount' => $this->getDiscount(),
			'Ppn' => '0',
			'Amount' => $this->getAmount()   
		);
 		
        $this->db->where(array('IDdetail' => $this->getIDdetail()));
        $this->db->update('transaction_faktur_product_pembelian', $data1);		
	}
	
	function deletefakturpembelian() {
        $data = array(
            'Active' => '0'
        );
        $this->db->where(array('NoFaktur' => $this->getNoFaktur()));
        $this->db->update('faktur_product_pembelian', $data);
    }
	
	
	function editquantheader() 
	{
		$data = array(
			'TotalQuantity' => $this->getTotalQuantity()
		);
		$this->db->where(array('NoFaktur' => $this->getNoFaktur()));
		$this->db->update('faktur_product_pembelian', $data);	
    }
	
	function deletefakturpembeliandetial() 
	{
		$data = array(
			'Active' => '0'
		);
		$this->db->where(array('IDdetail' => $this->getIDdetail()));
		$this->db->update('transaction_faktur_product_pembelian', $data);	
    }
	
	function getList(){
		$this->db->select("transaction_faktur_product_pembelian.NoFaktur,transaction_faktur_product_pembelian.NoPo,
		faktur_product_pembelian.DateFaktur,transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Discount,transaction_faktur_product_pembelian.Amount")
		->join('faktur_product_pembelian', 'faktur_product_pembelian.NoFaktur=transaction_faktur_product_pembelian.NoFaktur')
        ->join('supplier', 'supplier.Kodesupplier=return_product_pembelian.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=Transaction_return_product_pembelian.KodeDetailBarang')
		->where(array('transaction_faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()));
        $query = $this->db->get('transaction_faktur_product_pembelian');
        return $query->result();
	}
	
	function getlistnofaktur() {
        $this->db->select("faktur_product_pembelian.NoFaktur,faktur_product_pembelian.TotalQuantity,faktur_product_pembelian.TotalAmount,
		master_bank.KodeBank,faktur_product_pembelian.Bankname,master_bank.NoRekening")
		->join('master_bank', 'master_bank.Bankname=faktur_product_pembelian.Bankname')
		->join('supplier', 'supplier.Kodesupplier=return_product_pembelian.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=Transaction_return_product_pembelian.KodeDetailBarang')
       	->where(array('faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
    }
	
	function getListfakturdelay(){
		$this->db->select("transaction_faktur_product_pembelian.NoFaktur,transaction_faktur_product_pembelian.NoPo,
		faktur_product_pembelian.DateFaktur,order_product.Kodesupplier,supplier.suppliername,
transaction_faktur_product_pembelian.KodeDetailBarang,detail_product.Productname,detail_product.Typeunit,transaction_faktur_product_pembelian.Quantity,
		transaction_faktur_product_pembelian.Discount,transaction_faktur_product_pembelian.Amount")
		->join('faktur_product_pembelian', 'faktur_product_pembelian.NoFaktur=transaction_faktur_product_pembelian.NoFaktur')
        ->join('order_product', 'order_product.NoPo=transaction_faktur_product_pembelian.NoPo')
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
		->join('detail_product', 'detail_product.KodeDetailBarang=transaction_faktur_product_pembelian.KodeDetailBarang')
		->where(array('transaction_faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()));
        $query = $this->db->get('transaction_faktur_product_pembelian');
        return $query->result();
	}
	
	function getnofakturid() {
        $this->db->select()
        ->where(array('Active'=>'1'));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
    }
	
	function ambilnofaktur() {
        $this->db->select()
       	->where(array('faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()));
        $query = $this->db->get('faktur_product_pembelian');
        return $query->result();
    }
	
	function ambilnopoaja() {
        $this->db->select('NoPo')
       	->where(array('transaction_faktur_product_pembelian.NoFaktur' => $this->getNoFaktur()));
		$this->db->group_by('NoPo', 'asc');
        $query = $this->db->get('transaction_faktur_product_pembelian');
        return $query->result();
    }
	
	function ambilsupplier() 
	{
        $this->db->select("order_product.Kodesupplier,supplier.suppliername")
		->join('supplier', 'supplier.Kodesupplier=order_product.Kodesupplier')
       	->where(array('order_product.NoPo' => $this->getNoPo()));
        $query = $this->db->get('order_product');
        return $query->result();
    }
	
	function getTotalqty()
	{
		
		$query = $this->db->query("select TotalQuantity as totalquant from faktur_product_pembelian
			where NoFaktur='". $this->getNoFaktur() ."'");
		$row = $query->row();
		return $row->totalquant;
	}
}